<?php

namespace app\api\service\wanlshop;

use app\admin\model\UserIntroduce;
use app\api\model\wanlshop\WithdrawalGoods;
use think\Db;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\Exception;
use think\exception\DbException;
use think\Model;
use app\api\model\wanlshop\WithdrawalCash as WithdrawalCashModel;
use think\Validate;
use app\api\model\wanlshop\Goods;

// 提现
class WithdrawalCash extends Model
{
    // 提现model
    protected $withdrawalModel = null;

    public function __construct($data = [])
    {
        parent::__construct($data);
        $this->withdrawalModel = new WithdrawalCashModel();
    }

    /**
     *  提现
     */
    public function withdrawDepositService($params)
    {
        // 检测参数的合法性
        if (empty($params)) {
            throw new Exception('参数不能为空...', 2);
        }

        $rule = [
            'tracking_number' => 'require',
        ];

        $msg = [
            'tracking_number.require' => "物流单号不存在或不能为空"
        ];

        $v = new Validate($rule, $msg);
        if (!$v->check($params)) {
            throw new Exception($v->getError(), 4);
        }

        // 提现申请
        $amount_withdrawal = array_reduce(array_column($params['order']['gift'], 'price'), function ($carry, $item) {
            $carry += $item;
            return $carry;
        }, 0);
        $params['order']['amount_withdrawal'] = $amount_withdrawal;
        return $this->withdrawalModel->withdrawDepositModel($params);
    }


    /**
     *  活动提现记录添加
     */
    public function insWithdrawalRecord($params)
    {
        $this->withdrawalModel->insert($params);
    }


    /**
     *  活动提现记录列表
     */
    public function myWithdrawalCashLists($uid, $page, $pageSize)
    {
        return $this->withdrawalModel->getMyWithdrawalCashLists($uid, $page, $pageSize);
    }

    /**
     *  转介绍提现记录
     */
    /*public function introductionCashRecordService($uid, $page, $pageSize)
    {
        return $this->withdrawalModel->introductionCashRecordModel($uid, $page, $pageSize);
    }*/
}
